<?php
class Data_Model_DanhSachThiMapper extends \Data_Model_DataMapperAbstract {
	
	protected static $_instance = null;
	// dung de phan trang
	private $_paginator = null;
	
	/* public function searchByMahocvien($id)
	{
		$db = $this->getDb();
		$selectDiemthi = $db->select()
						->from('tbl_danhsachthi')
						->where('MaHocVien = ?',$id);
		$diemthis = $db->fetchAll($selectDiemthi);
		
		$diemthiArray = array();
		foreach($diemthis as $diemthi)
		{
			$lophocMapper = Data_Model_LopHocMapper::getInstance();
			$lophoc = $lophocMapper->searchById($diemthi['MaLop']);
			$diemthi['tbl_lophoc'] = $lophoc;
			
			$diemthiArray[] = $this->_populate($diemthi);
		}
		return $diemthiArray;
	} */
	public function searchByMalophoc($id)
	{
		$db = $this->getDb();
		$dsthiSelect = $db->select()
						->from('tbl_danhsachthi')
						->join('tbl_lophoc','tbl_lophoc.MaLop = tbl_danhsachthi.MaLop')
						->where('tbl_danhsachthi.MaLop = ?',$id);
		$dsthis = $db->fetchAll($dsthiSelect);
		$dsthiArray = array();
		foreach($dsthis as $dsthi)
		{
			$lophocMapper = Data_Model_LopHocMapper::getInstance();
			$lophoc = $lophocMapper->searchById($id);
			$dsthi['tbl_lophoc'] = $lophoc;
			
			$hocvienMapper = Data_Model_HocVienMapper::getInstance();
			$hocvien = $hocvienMapper->searchById($dsthi['MaHocVien']);
			$dsthi['tbl_hocvien'] = $hocvien;
	
			$dsthiArray[] =  $this->_populate($dsthi);
		}
		return $dsthiArray;
	}
	/* (non-PHPdoc)
	 * @see Data_Model_DataMapperAbstract::_insert()
	 */protected function _insert(Data_Model_DomainObjectAbstract $obj) {
		// TODO Auto-generated method stub
		$db = $this->getDb();
		$data = array(
				'MaHocVien' => $obj->getMahocvien(),
				'MaLop' => $obj->getMalop()
		);
		$db->insert('tbl_danhsachthi',$data);
		}

	/* (non-PHPdoc)
	 * @see Data_Model_DataMapperAbstract::_update()
	 */protected function _update(Data_Model_DomainObjectAbstract $obj) {
		// TODO Auto-generated method stub
		$db = $this->getDb();
		$data = array(
				'MaHocVien' => $obj->getMahocvien(),
				'MaLop' => $obj->getMalop()
		);
		$where = $db->quoteInto('ID = ?',$obj->getId());
		$db->update('tbl_danhsachthi',$data,$where);
		}

	/* (non-PHPdoc)
	 * @see Data_Model_DataMapperAbstract::_populate()
	 */protected function _populate($data) {
		// TODO Auto-generated method stub
		$obj = new Data_Model_DanhSachThi();
		$obj->setId($data['ID']);
		$obj->setMahocvien($data['MaHocVien']);
		$obj->setMalop($data['MaLop']);
		$obj->setLophoc($data['tbl_lophoc']);
		$obj->setHocvien($data['tbl_hocvien']);
		
		return $obj;
		}
		
		public function deleteByMahocvien($id)
		{
			$db = $this->getDb();
			$where = $db->quoteInto('MaHocVien = ?',$id);
			$db->delete('tbl_danhsachthi',$where);
		}
		
		// Goi chinh no
		
		public static function getInstance()
		{
			if(null === self::$_instance)
			{
				self::$_instance = new self();
			}
			return self::$_instance;
		}
}

?>